//
//  cCRegisterViewController.m
//  cyChat
//
//  Created by 楼楚 on 15/12/4.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "cCRegisterViewController.h"
#import "AppDelegate.h"



@interface cCRegisterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@end

@implementation cCRegisterViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.userField.background = [UIImage stretchedImageWithName:@"operationbox_text"];
    self.pwdField.background = [UIImage stretchedImageWithName:@"operationbox_text"];
    [self.registerBtn setBackgroundImage: [UIImage imageNamed:@"fts_green_btn"] forState:(UIControlStateNormal)];
    [self.registerBtn setBackgroundImage:[UIImage imageNamed:@"fts_green_btn_HL"] forState:(UIControlStateHighlighted)];
    
    
    
    
}
- (IBAction)registerBtnClick
{
   //隐藏键盘
    [self.view endEditing:YES];  
//    1.把用户注册的数据保存在单例
    
    cCUserInfo *userInfo = [cCUserInfo sharedcCUserInfo];
    userInfo.registerUser = self.userField.text;
    userInfo.registerPwd = self.pwdField.text;
    
    
   
//    2.调用AppDelegate的xmppUserRegister
  
//    cCXMPPTool *app = [UIApplication sharedApplication].delegate;
//    app.registerOperation = YES;
    [cCXMPPTool sharedcCXMPPTool].registerOperation = YES;
    
//      [MBProgressHUD showMessage:@"正在注册中..... " toView:self.view ];
    __weak typeof(self) selfVc = self;
    [[cCXMPPTool sharedcCXMPPTool] xmppUserRegister:^(XMPPResultType type) {
        
        
        [selfVc handleResultType:type];
    }];
}
/*
 
 处理注册的结果
 */

- (void)handleResultType:(XMPPResultType)type
{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //    [MBProgressHUD hideHUDForView:self.view];
        switch (type) {
            case XMPPResultTypeRegisterSuccess:
                //                    [MBProgressHUD showMessage:@"注册成功" toView:self.view];
                //回到上一个控制器
                [self dismissViewControllerAnimated:YES completion:nil];
                if ([self.delegate respondsToSelector:@selector(registerViewControllerDidFinshRegister)]) { [self.delegate registerViewControllerDidFinshRegister];
                    
                }
                break;
                
            case XMPPResultTypeRegisterFailue:
                NSLog(@"注册失败");
                //                    [MBProgressHUD showError:@"注册失败"toView:self.view ];
                break;
                
            case XMPPResultTypeNetErr:
                //                    [MBProgressHUD showError:@"网络炸啦" toView:self.view ];
            default:
                NSLog(@"break");
                break;
        }

    });
   

    //主线程刷新UI
    
    
    
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [MBProgressHUD hideHUDForView:self.view];
//        switch (type) {
//            case XMPPResultTypeRegisterSuccess:
//                [MBProgressHUD showMessage:@"注册成功" toView:self.view];
//            //回到上一个控制器
//                [self dismissViewControllerAnimated:YES completion:nil];
//                if ([self.delegate respondsToSelector:@selector(registerViewControllerDidFinshRegister)]) { [self.delegate registerViewControllerDidFinshRegister];
//                    NSLog(@"entermain");
//                }
//                break;
//            
//            case XMPPResultTypeRegisterFailue:
//                NSLog(@"注册失败");
//                [MBProgressHUD showError:@"注册失败"toView:self.view ];
//                break;
//            
//            case XMPPResultTypeNetErr:
//                [MBProgressHUD showError:@"网络炸啦" toView:self.view ];
//            default:
//                NSLog(@"break");
//                break;
//        }
//    });
}


- (IBAction)cancel:(id)sender {
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
